<?php
declare(strict_types=1);

namespace house;


use app\admin\model\BusinessArea;
use app\admin\model\House;
use app\admin\model\HouseAuction;
use app\admin\model\HouseVillage;
use app\admin\model\Region;
use custom\Log;
use think\Db;
use think\Exception;

/**
 * 编辑上架房源补录信息
 * 房源绑定的区域/小区/商圈字段发生变化后统计对应的房源数
 * Class HouseCount1
 * @package house
 */
class HouseCount1 extends HouseCount {

    public static $region_field1 = 'village_id';
    public static $region_field2 = 'business_area_id';
    public static $region_field3 = 'city_code';


    /**
     * 房源区域/小区/商圈更新后 统计 对应房源数
     * @param string $house_id
     * @param array $newData
     */
    public function __invoke(string $house_id, array $newData) {
        try {
            $house = (new House())->where('id', $house_id)->findOrEmpty()->toArray();
            $auction_status = (new HouseAuction())->where('id', $house['auction_id'])->value('auction_status');
            $house_village = new HouseVillage();
            $business_area = new BusinessArea();
            $region = new Region();
            //前提是上架房源
            if ($house['is_shelf'] == 1) {
                foreach ($newData as $k1 => $v1) {
                    /**
                     * 发生改变的地区相关的字段
                     * $v1：新的字段
                     * $house[$k1]：旧字段
                     */
                    if ($house[$k1] != $v1) {
                        switch ($k1) :
                            case self::$region_field1:
                                self::regionCountHandle($house_village, 'id', $auction_status, $house[$k1], $v1, 'sale_num', 'total_num');
                                break;
                            case self::$region_field2:
                                self::regionCountHandle($business_area, 'id', $auction_status, $house[$k1], $v1, 'house_number', 'total_number');
                                break;
                            case self::$region_field3:
                                self::regionCountHandle($region, 'city_code', $auction_status, $house[$k1], $v1, 'house_number', 'total_number');
                                break;
                            default:
                                break;
                        endswitch;
                    }
                }
            }
        } catch (Exception $e) {
            $err = "异常信息:{$e->getMessage()} | 异常所在的代码行:{$e->getLine()} | 异常文件{$e->getFile()} | php 版本 " . PHP_VERSION . "(" . PHP_OS . ")";
            Log::write($err, "HouseCount1_err.log");
        }
    }

    /**
     * @param $model
     * @param $column
     * @param $house_auction
     * @param $oldField
     * @param $v1
     * @param $sale_num
     * @param $total_num
     */
    public static function regionCountHandle($model, $column, $house_auction, $oldField, $v1, $sale_num, $total_num) {
        $old = $model->where($column, $oldField)->findOrEmpty()->toArray();
        //是待开始&进行中
        if (in_array($house_auction, [1, 2])) {
            //之前绑定的数据 扣1
            !empty($old) && $old[$sale_num] > 0 && $model->where($column, $oldField)->setDec($sale_num);
            //绑定的新数据 加1
            !$model->where($column, $v1)->findOrEmpty()->isEmpty() && $model->where($column, $v1)->setInc($sale_num);
        }
        //绑定的新数据 加1
        !$model->where($column, $v1)->findOrEmpty()->isEmpty() && $model->where($column, $v1)->setInc($total_num);
        //之前绑定的数据 扣1
        !empty($old) && $old[$total_num] > 0 && $model->where($column, $oldField)->setDec($total_num);
    }


}